import java.util.Scanner;

public class NGuessNumber {
    Scanner scan = new Scanner(System.in);
    int [] answer = new int[4];
    int [] guess = new int[4];
    String again;
    int a,b;
    
    public NGuessNumber(){
        do {
            generateAnswer();
            do {
                System.out.println("请输入4个0-9之间不重复的数字: ");
                do {
                    receiveGuess();
                }while (hasDuplicateGuess());
                getA();
                getB();
                if (a != 4){
                    System.out.println(a+"A"+b+"B");
                }else {
                    System.out.println("恭喜你猜对啦！");
                }
            }while (a != 4);
            System.out.println("再玩一次吗？ （y/n）");
            playAgain();
        }while (!again.equalsIgnoreCase("n"));
        scan.close();
    }

    public void generateAnswer(){
        int i = 0;
        while (i < 4){
            int candidate = (int) (Math.random()*10);
            boolean hasDuplicate = false;
            for (int j = 0;j < i; j++){
                if (answer[j] == candidate){
                    hasDuplicate = true;
                    break;
                }
            }
            if (hasDuplicate){
                continue;
            }else {
                answer[i] = candidate;
                i = i +1;
            }
        }
    }

    public void receiveGuess(){
        int i = 0;
        while (i < 4){
            try {
                guess[i] = scan.nextInt();
            }catch (Exception e){
                System.out.println("请输入4个0-9之间不重复的数字! ");
                scan.next();
                continue;
            }
            i = i + 1;
        }
    }

    public boolean hasDuplicateGuess(){
        for (int i = 0; i < 4; i++){
            for (int j = i + 1; j < 4; j++){
                if (guess[i] == guess[j]){
                    System.out.println("有重复的数字！请重新输入! ");
                    return true;
                }
            }
        }
        return false;
    }

    public int getA(){
        for (int i = 0; i < 4; i++){
            if (answer[i] == guess[i]){
                a = a +1 ;
            }
        }
        return a;
    }

    public int getB(){
        for (int i = 0; i < 4; i++){
            for (int j = 0; j < 4; j++){
                if (guess[j] == answer[i] && i != j){
                    b = b +1 ;
                }
            }
        }
        return b;
    }

    public void playAgain(){
        again = scan.next();
    }
    
    public static void main(String[] args) {
        NGuessNumber ngn = new NGuessNumber();
    }
}